using UnityEditor;
using UnityEngine;

namespace ParticleExporter
{
    [CustomPropertyDrawer(typeof(CanOverWriteValue<>))]
    public class CanOverWriteValueEditor : PropertyDrawer
    {
        private static readonly int height = 18;

        public override void OnGUI(Rect position, SerializedProperty property,
            GUIContent label)
        {
            var valueType = fieldInfo.FieldType.GenericTypeArguments[0];
            var overwritePpt = property.FindPropertyRelative("Overwrite");
            var valuePpt = property.FindPropertyRelative("Value");
            
            var currentLeft = position.x;
            Rect GetSpace(float ratio)
            {
                var width = position.width * ratio;
                var res = new Rect(currentLeft, position.y, width, height);
                currentLeft += width;
                return res;
            }

            EditorGUI.LabelField(GetSpace(0.3f), property.name);
            overwritePpt.boolValue = EditorGUI.Toggle(GetSpace(0.2f), overwritePpt.boolValue);
            if (overwritePpt.boolValue)
            {
                var rect = GetSpace(0.5f);
                if (valueType == typeof(int))
                {
                    valuePpt.intValue = EditorGUI.IntField(rect, valuePpt.intValue);
                }
                else if (valueType == typeof(float))
                {
                    valuePpt.floatValue = EditorGUI.FloatField(rect, valuePpt.floatValue);
                }
                else if (valueType == typeof(string))
                {
                    valuePpt.stringValue = EditorGUI.TextField(rect, valuePpt.stringValue);
                }
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return height;
        }
    }
}